<?php
namespace app\index\controller;

use think\Db;

class Index extends Base
{
    public function index()
    {
        return view('index');
    }
    public function index2()
    {
        return view();
    }
    public function input ()
    {
        return view();
    }
    public function item ()
    {
        return view('item2');
    }

    public function save_items ()
    {
        $post = input('post.');
        $hadIdCard = 0;
        $info = Db::connect($this->db2)->name('member')->where('idcard', $post['certificate'])->find();
        if (!empty($info) && $info['real_name'] == $post['name']) {
            $hadIdCard = 1;
            $info2 = db('member')->where('certificate' , $post['certificate'])->find();
            if (!$info2) {
                db('member')->insertGetId($post);
            }
        }else {
            // 第一个数据库不存在身份证，需要在本数据库查找看看
            $info = db('member')->where('certificate' , $post['certificate'])->find();
            if (!empty($info) && $info['name'] == $post['name']) {
                $hadIdCard = 1;
            }else {

                // 两个数据库都不存在，需要验证身份证是否正确
                // 身份证实名
                $url = 'http://op.juhe.cn/idcard/query?key=703f705fab1042d8c5295289d5e5cfab&idcard=' . $post['certificate'] . '&realname=' . $post['name'];
                $res = get($url);
                $res = json_decode($res, true);
                if ($res['error_code'] == 0) {
                    // 代表身份证验证成功，保存数据库
                    db('member')->insertGetId($post);
                } else {
                    return json(['status' => 0, 'msg' => '身份证不匹配']);
                }
            }
        }

        if ($hadIdCard) {
            session('name' , $post['name']);
            session('phone' , $post['phone']);
            session('certificate' , $post['certificate']);
            session('card' , $post['card']);
            session('quotas' , $post['quotas']);
            return json(['status' => 1]);
        }else {
            return json(['status' => 0, 'msg' => '审核出错，请联系客服']);
        }
    }

    public function erdu ()
    {
        return view();
    }
    public function shenhe ()
    {
        return view();
    }
    public function item2 ()
    {
        return view();
    }

    public function pay ()
    {
        $qrcode = db('config')->where('name' , 'qrcode')->find();
        $this->assign('qrcode' , $qrcode['value']);
        return view();
    }
    public function sure ()
    {
        return view();
    }

    public function build ()
    {
        $get = input('get.');
        $this->assign('get' , $get);
        return view();
    }
}
